package com.fjec.common.aliyun.api.view;

import com.google.common.base.Strings;
import org.apache.commons.collections.MapUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 阿里云营业执照识别返回数据
 * "config_str" : "null\n", #配置字符串信息
 * "angle" : float, #输入图片的角度（顺时针旋转），［0， 90， 180，270］
 * "reg_num" : string, #注册号，没有识别出来时返回"FailInRecognition"
 * "name" : string, #公司名称，没有识别出来时返回"FailInRecognition"
 * "type" : string, #公司类型，没有识别出来时返回"FailInRecognition"
 * "person" : string, #公司法人，没有识别出来时返回"FailInRecognition"
 * "establish_date": string, #公司注册日期(例：证件上为"2014年04月16日"，算法返回"20140416")
 * "valid_period": string, #公司营业期限终止日期(例：证件上为"2014年04月16日至2034年04月15日"，算法返回"20340415")
 * #当前算法将日期格式统一为输出为"年月日"(如"20391130"),并将"长期"表示为"29991231"，若证件上没有营业期限，则默认其为"长期",返回"29991231"。
 * "address" : string, #公司地址，没有识别出来时返回"FailInRecognition"
 * "capital" : string, #注册资本，没有识别出来时返回"FailInRecognition"
 * "business": string, #经营范围，没有识别出来时返回"FailInRecognition"
 * "emblem" : string, #国徽位置［top,left,height,width］，没有识别出来时返回"FailInDetection"
 * "title" : string, #标题位置［top,left,height,width］，没有识别出来时返回"FailInDetection"
 * "stamp" : string, #印章位置［top,left,height,width］，没有识别出来时返回"FailInDetection"
 * "qrcode" : string, #二维码位置［top,left,height,width］，没有识别出来时返回"FailInDetection"
 * "is_fake": false,   #是否是复印件
 * "success" : bool, #识别成功与否 true/false
 * "request_id": string
 */
public class BusinessLicenseVO extends HashMap<String, Object> {

    private final static String FAIL_IN_RECOGNITION = "FailInRecognition";
    private final static String SUCCESS_KEY = "success";
    private final static String NAME_KEY = "name";
    private final static String TYPE_KEY = "type";
    private final static String REG_NUM_KEY = "reg_num";
    private final static String BUSINESS_KEY = "business";
    private final static String CAPITAL_KEY = "capital";
    private final static String CAPTIAL_KEY = "captial";
    private final static String PERSON_KEY = "person";
    private final static String ADDRESS_KEY = "address";
    private final static String ESTABLISH_DATE_KEY = "establish_date";
    private final static String VALID_PERIOD_KEY = "valid_period";
    private final static String IS_FAKE_KEY = "is_fake";

    public static BusinessLicenseVO build(Map<String, Object> map) {
        BusinessLicenseVO vo = new BusinessLicenseVO();
        vo.putAll(map);
        return vo;
    }

    private BusinessLicenseVO() {
    }

    /**
     * 是否调用成功
     *
     * @return
     */
    public boolean isSuccess() {
        return MapUtils.getBooleanValue(this, SUCCESS_KEY);
    }

    /**
     * 公司名称
     *
     * @return
     */
    public String getName() {
        String name = MapUtils.getString(this, NAME_KEY);
        if (!Objects.equals(FAIL_IN_RECOGNITION, name)) {
            return name;
        } else {
            return null;
        }
    }

    /**
     * 公司类型
     *
     * @return
     */
    public String getType() {
        String type = MapUtils.getString(this, TYPE_KEY);
        if (!Objects.equals(FAIL_IN_RECOGNITION, type)) {
            return type;
        } else {
            return null;
        }
    }

    /**
     * 公司注册号
     *
     * @return
     */
    public String getRegNum() {
        String regNum = MapUtils.getString(this, REG_NUM_KEY);
        if (!Objects.equals(FAIL_IN_RECOGNITION, regNum)) {
            return regNum;
        } else {
            return null;
        }
    }

    /**
     * 公司经营范围
     *
     * @return
     */
    public String getBusiness() {
        String business = MapUtils.getString(this, BUSINESS_KEY);
        if (!Objects.equals(FAIL_IN_RECOGNITION, business)) {
            return business;
        } else {
            return null;
        }
    }

    /**
     * 公司注册资本
     *
     * @return
     */
    public String getCapital() {
        // 以API文档为主，如果capital为空则取captial值
        String capital = MapUtils.getString(this, CAPITAL_KEY);
        String captial = MapUtils.getString(this, CAPTIAL_KEY);
        if (Strings.isNullOrEmpty(capital)) {
            capital = captial;
        }
        if (!Objects.equals(FAIL_IN_RECOGNITION, capital)) {
            return capital;
        } else {
            return null;
        }
    }

    /**
     * 公司法人
     *
     * @return
     */
    public String getPerson() {
        String person = MapUtils.getString(this, PERSON_KEY);
        if (!Objects.equals(FAIL_IN_RECOGNITION, person)) {
            return person;
        } else {
            return null;
        }
    }

    /**
     * 公司地址
     *
     * @return
     */
    public String getAddress() {
        String addr = MapUtils.getString(this, ADDRESS_KEY);
        if (!Objects.equals(FAIL_IN_RECOGNITION, addr)) {
            return addr;
        } else {
            return null;
        }
    }

    /**
     * 公司注册日期
     *
     * @return
     */
    public String getEstablishDate() {
        String establishDate = MapUtils.getString(this, ESTABLISH_DATE_KEY);
        if (!Objects.equals(FAIL_IN_RECOGNITION, establishDate)) {
            return establishDate;
        } else {
            return null;
        }
    }

    /**
     * 公司营业期限终止日期
     *
     * @return
     */
    public String getValidPeriod() {
        String validPeriod = MapUtils.getString(this, VALID_PERIOD_KEY);
        if (!Objects.equals(FAIL_IN_RECOGNITION, validPeriod)) {
            return validPeriod;
        } else {
            return null;
        }
    }

    /**
     * 是否是复印件
     *
     * @return
     */
    public boolean isFake() {
        return MapUtils.getBooleanValue(this, IS_FAKE_KEY);
    }


    /**
     * 校验证件有效性
     *
     * @return
     */
    public boolean isValid() {
        if (Strings.isNullOrEmpty(getRegNum()) || Strings.isNullOrEmpty(getName())
                || Strings.isNullOrEmpty(getType()) || Strings.isNullOrEmpty(getPerson())
                || Strings.isNullOrEmpty(getAddress()) || Strings.isNullOrEmpty(getCapital())
                || isFake()) {
            return false;
        } else {
            return true;
        }
    }
}
